<?php
require_once "./db.php";
header("Content-Type: application/json");

$id = @$_GET["id"];
$score = @$_GET["score"];

$res = [
    "code" => 200,
    "message" => "ok"
];

if(!isset($id)){
    $res["code"] = 4000;
    $res["message"] = "place input id";
    echo json_encode($res);die();
}

if(!isset($score)){
    $res["code"] = 4000;
    $res["message"] = "place input score";
    echo json_encode($res);die();
}
$id = (int)$id;
$score = (int)$score;

$query_sql = "select * from player where id = " .$id;
$query_stmt = $pdo->query($query_sql);
if(!$query_stmt->rowCount()){
    $res["code"] = 4001;
    $res["message"] = "id not exists in db";
    echo json_encode($res);die();
}
$sql = "update player set score = " . $score . " where id = " . $id;

if (!$pdo->exec($sql)) {
    $res["code"] = 5000;
    $res["message"] = "insert error";
    echo json_encode($res);die();
}
echo json_encode($res);
